package com.qxsx.midi.activity.personal;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2015/9/8.
 */
public class PersonalEditNickNameActivity extends BaseActivity implements View.OnClickListener{
    private RelativeLayout rl_mess,rl_name,rl_email,rl_position,rl_password,rl_phonenum;
    private LinearLayout ll_back,ll_save;
    private EditText ed_name;
    Intent intent = new Intent();
    private String userid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_nickname);
        setTitle("昵称");
        setNavBtn(R.mipmap.back, null, 0, "保存");

        ed_name = (EditText)findViewById(R.id.ed_name_name);
        userid = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        String usernick= (String) SPUtils.get(AppContext.getContext(), "user_nickname", "");
        if(usernick==null){
            usernick="";
        }
        ed_name.setText(usernick);

    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
            case RIGHT:
                final String name= ed_name.getText().toString().trim();
                if(name!=null&&name!=""){
//                    intent.setClass(AppContext.getContext(),PersonalDetailsActivity.class);
//                    startActivity(intent);
                    JSONArray array = new JSONArray();
                    JSONObject object = new JSONObject();
                    try {
                        object.put("user_id",userid);
                        object.put("nickname", name);
                        array.put(object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    MyNet.doPostJsonArray("106", array, new callBackJsonArray() {
                                @Override
                                public void run(Object code, Object message, JSONArray array) {
                                    // Log.e("-------------",code.toString());
                                    if (code.toString().equals("401")) {
                                        Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                    } else if (array == null) {
                                        Log.w("array", array + "");
                                        return;
                                    } else {
                                        SPUtils.put(AppContext.getContext(), "user_nickname", name);
                                        Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                        finish();
                                    }
                                }
                            }

                    );
                    //finish();
                }
                break;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

    public static int dip2px(Context context, float dipValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }

    // 像素转 dpi
    public static int px2dip(Context context, float pxValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5f);
    }
}
